package com.item.controller;

import com.item.bean.RequestUserName;
import com.item.constant.RedisMobileMessageConstant;
import com.item.domain.TUsers;
import com.item.domain.TbUser;
import com.item.service.TUsersService;
import com.item.service.TbUserService;
import com.item.utils.JWTUtils;
import com.item.utils.SMSUtils;
import com.item.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.concurrent.TimeUnit;

@RestController
public class RegistoryController {


    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private TUsersService usersService;

    @Autowired
    private TbUserService tbUserService;

    /**
     * 手机app获取验证码接口
     *
     * @param phone
     * @return
     */
    @GetMapping(value = "/phone/registory/{phone}")
    public String phoneRegistory(@PathVariable(value = "phone") String phone) {

        //后台做个redis判断
        Object gubangjie = redisTemplate.opsForValue().get(phone + RedisMobileMessageConstant.SENDTYPE_LOGIN);//这个是获取redis里面的值
        if (gubangjie != null) {
            return gubangjie.toString();
        }
        //校验
        if (phone != null || phone.equals("")) {

            //Integer integer = ValidateCodeUtils.generateValidateCode(4);
            Integer integer = 8888;
            try {
                //调用阿里云
                //SMSUtils.sendShortMessage(phone,integer.toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
            //发送成功后把验证码存入到redis中
            //第二种有限制时间的,,,,,
            redisTemplate.opsForValue().set(phone + RedisMobileMessageConstant.SENDTYPE_LOGIN, integer.toString(), 60, TimeUnit.SECONDS); //TimeUnit.SECONDS意思是线程等待5秒
            //获取redis里面的值
            gubangjie = redisTemplate.opsForValue().get(phone + RedisMobileMessageConstant.SENDTYPE_LOGIN);//这个是获取redis里面的值
            System.out.println(gubangjie.toString());
            if (gubangjie != null) {
                return gubangjie.toString();
            }
        }


        return "验证码获取失败";

    }

    /**
     * 手机app登录接口
     *
     * @param phone
     * @param code
     * @return
     */
    @PostMapping(value = "/login/code/{phone}/{code}")
    public RequestUserName loginCode(@PathVariable(value = "phone") String phone, @PathVariable(value = "code") String code) {
        RequestUserName requestUserName = new RequestUserName();

        //登录业务逻辑
        //1,使用手机号查询数据库是否有改用户的信息,如果有就返回
        //TUsers users =  usersService.findAllUsers(phone);
        TbUser tbUser = tbUserService.findAllUsers(phone);
        //生成token
        String token = JWTUtils.geneJsonWebToken(tbUser);
        //存入redis
        redisTemplate.opsForValue().set(phone + RedisMobileMessageConstant.SENDTYPE_TOKEN, token.toString(), 1000 * 60 * 60 * 24, TimeUnit.SECONDS);

        if (phone != null && code != null) {
            Object gubangjie = redisTemplate.opsForValue().get(phone + RedisMobileMessageConstant.SENDTYPE_LOGIN);//这个是获取redis里面的值 Object gubangjie = redisTemplate.opsForValue().get(phone+ RedisMobileMessageConstant.SENDTYPE_LOGIN);//这个是获取redis里面的值
            if (phone.equals(tbUser.getMobile()) && code.equals(gubangjie)) {
                return new RequestUserName(true, "请求成功", tbUser.getId(), token, tbUser.getMobile(), tbUser.getHeader());
            }
        } else {
            return new RequestUserName(false);
        }

        return new RequestUserName("验证码输入有误");
    }


    @RequestMapping(value = "/user/info/add/mobile")
    public RequestUserName userInfoAddMobile(@RequestBody TbUser users) {


        int i = tbUserService.userInfoAddMobile(users);
        if (i == 1) {
            return new RequestUserName(true);
        }
        return new RequestUserName(false);


    }

    @PostMapping(value = "/userInfo/findAdd")
    public RequestUserName userInfoFindAdd(@RequestBody TUsers users) {
        users.setCreateTime(System.currentTimeMillis());
        int i = usersService.userInfoAddMobileUpdate(users);
        if (i == 1) {
            return new RequestUserName(true);
        }
        return new RequestUserName(false);
    }


}
